package com.lab.idea.bl.entity;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.lab.idea.mybatis.entity.LabEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * <p>
 * 输出查询sql
 * </p>
 *
 * @author lab
 * @since 2023-11-09
 */
@Getter
@Setter
@Accessors(chain = true)
@TableName("bl_excel_query")
@ApiModel(value = "ExcelQuery对象", description = "输出查询sql")
public class ExcelQuery extends LabEntity<ExcelQuery> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "queryId", type = IdType.ASSIGN_ID)
    private String queryId;

    @TableField("queryName")
    private String queryName;

    @TableField("querySql")
    private String querySql;

    @TableField("joinProp")
    private String joinProp;

    @TableField("outId")
    private String outId;

    @TableField("excelId")
    private String excelId;

    @TableField("queryMain")
    private Boolean queryMain;

    @TableField("matchColumn")
    private String matchColumn;

    @TableField("mapperColumn")
    private String mapperColumn;

    public List<String> toList(String jsonArray){
        List<String> list = new ArrayList<>();
        if (StrUtil.isBlankIfStr(jsonArray)) {
            return list;
        }
        JSONArray array = JSONUtil.parseArray(jsonArray);
        for (int i = 0; i < array.size(); i++) {
            list.add(array.getStr(i));
        }
        return list;
    }

    public String toFormat(String jsonArray){
        List<String> list = toList(jsonArray);
        if (CollUtil.isEmpty(list)) {
            return StrUtil.EMPTY;
        }
        StringBuilder builder = new StringBuilder();
        for (String s : list) {
            builder.append("${").append(s).append("}").append("@");
        }
        return builder.toString();
    }

    @Override
    public Serializable pkVal() {
        return this.queryId;
    }
}
